package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.*;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.*;
import com.itheima.health.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@DubboService
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealGroupMapper setmealGroupMapper;
    @Autowired
    CheckGroupMapper checkGroupMapper;
    @Autowired
    CheckGroupItemMapper checkGroupItemMapper;
    @Autowired
    CheckItemMapper checkItemMapper;

    /**
     * @Author: JASON
     * @Date: 9:12 2021/1/6
     * @Parms [queryPageBean]
     * @ReturnType: com.itheima.health.entity.PageResult
     * @Description: 套餐管理分页查询
    */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("Service套餐分页查询"+queryPageBean);
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //创建条件
        Example example = Example.builder(Setmeal.class).build();
        Example.Criteria criteria = example.createCriteria();
        String queryString = queryPageBean.getQueryString();
        if (!StringUtils.isEmpty(queryString)) {
            criteria.orLike("name", "%" + queryString+ "%");
            criteria.orLike("code", "%" + queryString+ "%");
            criteria.orLike("helpCode", "%" + queryString+ "%");
        }
        Page<Setmeal> page = (Page<Setmeal>) setmealMapper.selectByExample(example);
        //Page<Setmeal> page = setmealMapper.findPage(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    /**
     * @Author: JASON
     * @Date: 9:39 2021/1/6
     * @Parms [setmeal, checkgroupIds]
     * @ReturnType: void
     * @Description: 套餐管理新增
    */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("Service套餐管理"+setmeal+"检查组id"+checkgroupIds.toString());
        //保存套餐
        setmealMapper.insertSelective(setmeal);
        //保存中间表 遍历检查组id  setmeal.getId()获取回显主键
        SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
        setmealCheckgroup.setSetmealId(setmeal.getId());
        for (Integer checkgroupId : checkgroupIds) {
            setmealCheckgroup.setCheckgroupId(checkgroupId);
            //SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup(setmeal.getId(), checkgroupId);
            setmealGroupMapper.insertSelective(setmealCheckgroup);
        }
        //图片不为空则操作redis进行记录
    }

    /**
     * @Author: JASON
     * @Date: 10:09 2021/1/6
     * @Parms [id]
     * @ReturnType: com.itheima.health.pojo.Setmeal
     * @Description: 根据id查询套餐
    */
    @Override
    public Setmeal findById(Integer id) {
        log.info("根据id查询套餐"+id);
        //查询基本信息
        Setmeal setmeal = setmealMapper.selectByPrimaryKey(id);
        //查询中间表checkGroupIDs
        SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
        setmealCheckgroup.setSetmealId(id);
        List<SetmealCheckgroup> select = setmealGroupMapper.select(setmealCheckgroup);
        ArrayList<Integer> checkGroupIds = new ArrayList<>();
        for (SetmealCheckgroup checkgroup : select) {
            checkGroupIds.add(checkgroup.getCheckgroupId());
        }
        //通过遍历checkGroupIds在checkgroup表中查询checkgroup 封装setmeal中的CheckGroups属性
        ArrayList<CheckGroup> checkGroups = new ArrayList<>();
        for (Integer checkGroupId : checkGroupIds) {
            ArrayList<CheckItem> checkItems = new ArrayList<>();
            CheckGroup checkGroup = checkGroupMapper.selectByPrimaryKey(checkGroupId);
            //通过CheckGroupId查询CheckItem 封装到CheckGroup中的CheckItems
            List<Integer> checkItemIds= checkGroupItemMapper.findCheckItemIdsByCheckGroupId(checkGroupId);
            for (Integer checkItemId : checkItemIds) {
                CheckItem checkItem = checkItemMapper.selectByPrimaryKey(checkItemId);
                checkItems.add(checkItem);
            }
            checkGroup.setCheckItems(checkItems);
            log.info("封装后的checkGroup为"+checkGroup);
            checkGroups.add(checkGroup);
        }
        setmeal.setCheckGroups(checkGroups);
        log.info("封装后的setmeal"+setmeal);
        return setmeal;
    }

    /**
     * @Author: JASON
     * @Date: 15:47 2021/1/8
     * @Parms [id]
     * @ReturnType: java.util.List<java.lang.Integer>
     * @Description: 通过套餐id查询检查组ids
    */
    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {
        SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
        setmealCheckgroup.setSetmealId(id);
        List<SetmealCheckgroup> select = setmealGroupMapper.select(setmealCheckgroup);
        ArrayList<Integer> integers = new ArrayList<>();
        for (SetmealCheckgroup checkgroup : select) {
            integers.add(checkgroup.getCheckgroupId());
        }
        return integers;
    }

    /**
     * @Author: JASON
     * @Date: 10:00 2021/1/9
     * @Parms [fileName]
     * @ReturnType: int
     * @Description: 根据文件名称查询
    */
    @Override
    public int count(String fileName) {
        Setmeal setmeal = new Setmeal();
        setmeal.setImg(fileName);
        int count = setmealMapper.selectCount(setmeal);
        return count;
    }

    /**
     * @Author: JASON
     * @Date: 17:29 2021/1/9
     * @Parms []
     * @ReturnType: java.util.List<com.itheima.health.pojo.Setmeal>
     * @Description: 套餐查询所有
    */
    @Override
    public List<Setmeal> findAll() {
        return setmealMapper.selectAll();
    }

}
